/**
 * 桨模 列表页
 *
 * 作者 ganjun
 * 日期 2024-11-18 22:28:43
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        laydate = layui.laydate,
        $view = $('#febs-modelOars'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#modelOars-table-form'),
        $statusSearch = $view.find('#statusSearchSelect'),
        tableIns;

    form.verify(validate);
    form.render();
    //状态
    febs.initDictItemSelect($statusSearch, 'MODEL_OARS_STATUS', null, null);
    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'modelOarsTable',
            url: ctx + 'modelOars/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                // {title: '序号', type: 'numbers',width: 60},
                {field: 'projectCode', title: '项目编号', minWidth: 120},
                {field: 'projectName', title: '项目名称', minWidth: 120},
                {field: 'oarsCode', title: '桨模编号', minWidth: 100},
                {field: 'oarsName', title: '名称', minWidth: 100},
                {field: 'sourceName', title: '来源', minWidth: 80},
                {field: 'statusName', title: '状态', minWidth: 80},
                {field: 'demandEnddate', title: '要求完成时间', minWidth: 120},
                {field: 'actuallyStartdate', title: '实际开始时间', minWidth: 120},
                {field: 'actuallyEnddate', title: '实际完成时间', minWidth: 120},
                {title: '操作', toolbar: '#modelOars-tool', width: 270,align:'center',fixed:'right'}

            ]],
            toolbar: '#modelOars-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(modelOarsTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(modelOarsTable)', function (obj) {
        if (obj.event === 'add') {
            parent.layui.febs.navigate('/modelOars/add');
            /*febs.modal.open('新增', 'modelOars/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? ['90%', '90%'] : ['80%', '80%'],
                yes: function (index, layero) {
                    $('#modelOars-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                }
            });*/
        }
        //导出
        if(obj.event === 'export'){
            var params = getQueryParams();
            febs.download(ctx + 'modelOars/export', params, '桨模信息表.xls');
        }
    });

    table.on('tool(modelOarsTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.id);
            });
        }
        if (layEvent === 'edit') {
            parent.layui.febs.navigate('/modelOars/update/' + data.id);
            /*febs.modal.open('修改', 'modelOars/update/' + data.id, {
                area: $(window).width() <= 750 ? ['90%', '90%'] : ['80%', '80%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#modelOars-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });*/
        }


        if (layEvent === 'finish') {
            var subHeight = '70%';
            if(data.source === 2 || data.source === 3){
                subHeight = '40%';
            }
            febs.modal.open('完成', 'modelOars/finish/' + data.id, {
                area: $(window).width() <= 750 ? ['45%', subHeight] : ['45%', subHeight],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#modelOars-finish').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        }

        if (layEvent === 'view') {
            parent.layui.febs.navigate('/modelOars/detail/' + data.id);
            /*febs.modal.open('查看', 'modelOars/detail/' + data.id, {
                area: $(window).width() <= 750 ? ['90%', '90%'] : ['70%', '70%'],
                btn: [ '取消'],
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });*/
        }

    });

    // $delete.on('click', function () {
    //     var checkStatus = table.checkStatus('modelOarsTable');
    //     if (!checkStatus.data.length) {
    //         febs.alert.warn('没有选择要删除的数据！');
    //     } else {
    //         febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
    //             var ids = [];
    //             layui.each(checkStatus.data, function (key, item) {
    //                 ids.push(item.id);
    //             });
    //             deleteIds(ids.join(','));
    //         });
    //     }
    // });

    function getQueryParams() {
        return {
            name: $searchForm.find('input[name="name"]').val().trim(),
            demondFinishTime: $searchForm.find('input[name="demondFinishTime"]').val().trim(),
            statusSearch: $searchForm.find("select[name='statusSearch']").find("option:selected").val(),
            oarsCodeSearch: $searchForm.find('input[name="oarsCodeSearch"]').val().trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'modelOars/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

    laydate.render({
        elem: "#demondFinishTime",
        type: 'datetime',
        range: true,
        format: 'yyyy-MM-dd HH:mm',
    });

});